//
//  KWNFeedbackViewController.m
//  KWNClient
//
//  Created by 张明磊 on 14/10/30.
//  Copyright (c) 2014年 张明磊. All rights reserved.
//

#import "KWNFeedbackViewController.h"
#import "KWNNetwork.h"
#import "KWLLoginViewController.h"

@interface KWNFeedbackViewController ()<UITextViewDelegate,UIAlertViewDelegate>

@property (weak, nonatomic) UITextView *textView_content;
@property (weak, nonatomic) UILabel *label_count;
@property (strong, nonatomic) NSString *questionId;

@end

@implementation KWNFeedbackViewController

- (id)initWithQid:(NSString *)questionid;
{
    self = [super init];
    if (self) {
        self.questionId = questionid;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self UI_feedback];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"feedback"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"feedback"];
}

-(void)UI_feedback{
    self.view.backgroundColor = RGB(227, 227, 227);
    
    UIView *imageView_title = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44 + IOS7_SIZE)];
    imageView_title.backgroundColor = RGB(91, 165, 229);
    [self.view addSubview:imageView_title];
    
    UIButton *button_menu = [UIButton buttonWithType:UIButtonTypeCustom];
    button_menu.frame = CGRectMake(0, IOS7_SIZE, 60, 45);
    [button_menu setImage:[UIImage imageFileName:@"button_back.png"] forState:UIControlStateNormal];
    [button_menu addTarget:self action:@selector(didClickButton_feedback:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_menu];
    button_menu.tag = 100;
    
    UIButton *button_login = [UIButton buttonWithType:UIButtonTypeCustom];
    button_login.frame = CGRectMake(250, IOS7_SIZE, 60, 45);
    [button_login setTitle:@"提交" forState:UIControlStateNormal];
    [button_login setTitleColor:ColorOfTitle forState:UIControlStateNormal];
    button_login.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
    [button_login addTarget:self action:@selector(didClickButton_feedback:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_login];
    button_login.tag = 101;
    
    UILabel *label_title = [[UILabel alloc] initWithFrame:CGRectMake(imageView_title.frame.origin.x,IOS7_SIZE + 5,imageView_title.frame.size.width,35)];
    label_title.backgroundColor = [UIColor clearColor];
    label_title.textColor = ColorOfTitle;
    label_title.textAlignment = NSTextAlignmentCenter;
    label_title.font = [UIFont fontWithName:@"Helvetica-Bold" size:16.0f];
    [self.view addSubview:label_title];
    label_title.text = @"举报问题";
    
    UIView *view_input = [[UIView alloc] initWithFrame:CGRectMake(0, imageView_title.frame.origin.y + imageView_title.frame.size.height,self.view.frame.size.width, self.view.frame.size.height - imageView_title.frame.size.height - imageView_title.frame.origin.y - Iphone5_size(278, 300))];
    view_input.backgroundColor = [UIColor clearColor];
    [self.view addSubview:view_input];
    
    UIView *view_board = [[UIView alloc] initWithFrame:CGRectMake(10, Iphone5_size(10, 20), view_input.frame.size.width - 20, view_input.frame.size.height - 20)];
    view_board.backgroundColor = [UIColor whiteColor];
    view_board.layer.cornerRadius = 6.0;
    view_board.layer.borderWidth = 1.0;//边框宽度
    view_board.layer.borderColor = RGB(197, 197, 197).CGColor;//边框颜色
    [view_input addSubview:view_board];
    
    UITextView *textView_input = [[UITextView alloc] initWithFrame:CGRectMake(20, view_board.frame.origin.y, view_board.frame.size.width - 20, view_board.frame.size.height - Iphone5_size(42, 55))];
    textView_input.backgroundColor = [UIColor clearColor];
    textView_input.text = @"请描述您举报的内容";
    textView_input.textColor = RGB(211, 211, 211);
    textView_input.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    textView_input.autocorrectionType = UITextAutocorrectionTypeNo;
    textView_input.autocapitalizationType = UITextAutocapitalizationTypeNone;
    [view_input addSubview:textView_input];
    textView_input.delegate = self;
    _textView_content = textView_input;
    textView_input.tag = 200;
    [textView_input becomeFirstResponder];
    
    UIButton *button_closeKey = [UIButton buttonWithType:UIButtonTypeCustom];
    button_closeKey.frame = CGRectMake(0, 0, 320, 30);
    [button_closeKey setImage:[UIImage imageFileName:@"button_closeKey.png"] forState:UIControlStateNormal];
    [button_closeKey addTarget:self action:@selector(didClickButton_feedback:)forControlEvents:UIControlEventTouchUpInside];
    [textView_input setInputAccessoryView:button_closeKey];
    button_closeKey.tag = 102;
}

- (void)didClickButton_feedback:(UIButton *)button{
    switch (button.tag) {
        case 100:{
            [MobClick event:@"feedback_back"];
            [self popViewController];
        }break;
        case 101:{
            [MobClick event:@"feedback_send"];
            [self.view endEditing:YES];

            if(!_textView_content || _textView_content.text.length == 0){
// CS 快法务 KLW 2014-12-24
//                [UIUtils view_showProgressHUD:@"内容不能为空" inView:self.view withTime:1.5f];
//                
                [SVProgressHUD showErrorWithStatus:@"内容不能为空" duration:1.5];
// CE 快法务 KLW 2014-12-24
            }
            else{
                
// CS 快法务 KLW 2014-12-24
//                [UIUtils view_showProgressHUD:@"加载中,请稍候..." inView:self.view withType:0];
//                
                [SVProgressHUD showWithStatus:@"加载中,请稍候..." maskType:SVProgressHUDMaskTypeClear];
// CE 快法务 KLW 2014-12-24
                [[KWNNetwork sharedInstance]reportWithQuestionId:_questionId andReportStr:_textView_content.text FinishBlock:^(NSInteger stateCode, NSMutableArray *result) {
// DS 快法务 KLW 2014-12-24
//                    [UIUtils view_hideProgressHUDinView:self.view];
//
// DE 快法务 KLW 2014-12-24
                    switch (stateCode) {
                        case 10000:
                        {
// CS 快法务 KLW 2014-12-24
//                            [UIUtils view_showProgressHUD:@"您的举报信息提交成功" inView:self.view withTime:1.5f];
//                            
                            [SVProgressHUD dismissWithSuccess:@"您的举报信息提交成功" afterDelay:1.5];
// CE 快法务 KLW 2014-12-24
                            double delayInSeconds = 1.5;
                            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                                // code to be executed on the main queue after delay
                                [self popViewController];
                            });

                        }
                            break;
                        case 10003:
                        {
// AS 快法务 KLW 2014-12-24
                            [SVProgressHUD dismiss];
// AE 快法务 KLW 2014-12-24
                            UIAlertView *alter_login;
                            if(IOS8_VERSION){
                                alter_login = [[UIAlertView alloc] initWithTitle:@"您的来问律师账号已在其它地方登录，请您重新登录此设备。如果不是您的操作，您的密码可能已泄露，请及时更改密码，或联系快问律师客服邮箱laiwenlvshi@kuaifawu.com。" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                            }
                            else{
                                alter_login = [[UIAlertView alloc] initWithTitle:nil message:@"您的来问律师账号已在其它地方登录，请您重新登录此设备。如果不是您的操作，您的密码可能已泄露，请及时更改密码，或联系快问律师客服邮箱laiwenlvshi@kuaifawu.com。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                            }
                            alter_login.tag = 10003;
                            [alter_login show];
                            
                            //                            NSString *string_msg  = result[0];
                            //                            [UIUtils view_showProgressHUD:string_msg inView:self.view withTime:1.5f];
                            //
                            //                            double delayInSeconds = 1.5;
                            //                            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                            //                            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                            //                                // code to be executed on the main queue after delay
                            //                                [self ifUnloginPostNotificaToPushLoginView];
                            //                            });
                        }
                            break;
                        default:
                        {
                            NSString *string_msg  = result[0];
// CS 快法务 KLW 2014-12-24
//                            [UIUtils view_showProgressHUD:string_msg inView:self.view withTime:1.5f];
//                            
                            [SVProgressHUD dismissWithError:string_msg afterDelay:1.5];
// CE 快法务 KLW 2014-12-24
                        }
                            break;
                    }
                }];

            }
        }break;
        case 102:{
            [MobClick event:@"feedback_closekey"];

            [self.view endEditing:YES];
        }break;
        default:
            break;
    }
}

#pragma mark - 输入代理 -
/**
 *  输入代理
 *
 *  @param textView 输入代理
 */
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if(text && textView.tag == 200){
        textView.textColor = RGB(109, 109, 109);
        textView.text = @"";
        textView.tag = 201;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView{
}

#pragma mark - 点击 -
/**
 *  点击
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:{
            [self popViewController];
        }break;
        case 1:{
            [self popViewController];
        }break;
        case 10003:
        {
            KWLLoginViewController *login = [[KWLLoginViewController alloc] initWithTag:6];
            [self pushViewController:login];
        }
            break;
        default:
            break;
    }
}

@end
